﻿using System.Threading;
using System.Windows;
using jSLApp.Client.Workflow;
using jSLApp.Web;
using jSLCore.Client.Presentation.BaseControl.DialogMessage;

namespace jSLApp.Client.Forms.ShowCases
{
    [ShowCase("DataControl","03. Forms",20)]
    public partial class DataControlEx
    {
        public DataControlEx()
        {
            InitializeComponent();
        }

        private void doWork_Clicked(object sender, RoutedEventArgs e)
        {
            DoWork("DoWork test", (o, args) =>
                                      {
                                          Thread.Sleep(3000);//simulating
                                          args.Result = args.Params[1];
                                      }, (o, args) =>
                                             {
                                                 if(args.Error==null)
                                                 {
                                                     InfoBox.ShowDialog(args.Result.ToString());
                                                 }
                                             },"Argument1","Argument2","ArgumentN");
        }

        private void doDomainContextWork_Clicked(object sender, RoutedEventArgs e)
        {
            var domainContext = new MainDomainContext();

            DoWorkOnVoidOperation("Calling Operation with no return value",
                service => service.WaitOnServer(3),
                hasError =>
                    {
                        if (!hasError)
                        {
                            InfoBox.ShowDialog("There was no error");
                        }
                    }, domainContext);
        }

        private double _b = 2;

        private void doDomainContextWorkAndGetValue_Clicked(object sender, RoutedEventArgs e)
        {
            const double a = 3;
            this.DoWorkOnOperation("Dividing",context => context.Devide(a,_b),(hasError, result) =>
                                                                                 {
                                                                                     if(!hasError)
                                                                                     {
                                                                                         InfoBox.ShowDialog(result.ToString());
                                                                                     }
                                                                                 });
            _b = _b - 1;
        }
    }
}
